package com.belf.web.controller.console;

import com.belf.common.annotation.Log;
import com.belf.common.core.controller.BaseController;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.core.domain.entity.SysUser;
import com.belf.common.core.page.TableDataInfo;
import com.belf.common.enums.BusinessType;
import com.belf.common.utils.SecurityUtils;
import com.belf.common.utils.poi.ExcelUtil;
import com.belf.console.domain.DataSourceInfo;
import com.belf.console.service.IDataSourceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据源信息Controller
 * 
 * @author belf
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/console/datasourceInfo")
public class DataSourceInfoController extends BaseController
{
    @Autowired
    private IDataSourceInfoService dataSourceInfoService;

    /**
     * 查询数据源信息列表
     */
    @PreAuthorize("@ss.hasPermi('console:datasourceInfo:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody DataSourceInfo dataSourceInfo)
    {
        // 管理员拥有所有权限
        if (!SysUser.isAdmin(SecurityUtils.getUserId())){
            dataSourceInfo.setCreateBy(getUsername());
        }
        List<DataSourceInfo> list = dataSourceInfoService.selectDataSourceInfoList(dataSourceInfo);
        long count = dataSourceInfoService.selectDataCount(dataSourceInfo);
        return getDataTablePost(list,count);
    }

    /**
     * 导出数据源信息列表
     */
    @PreAuthorize("@ss.hasPermi('console:datasourceInfo:export')")
    @Log(title = "数据源信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataSourceInfo dataSourceInfo)
    {
        List<DataSourceInfo> list = dataSourceInfoService.selectDataSourceInfoList(dataSourceInfo);
        ExcelUtil<DataSourceInfo> util = new ExcelUtil<DataSourceInfo>(DataSourceInfo.class);
        util.exportExcel(response, list, "数据源信息数据");
    }

    /**
     * 获取数据源信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('console:datasourceInfo:query')")
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody DataSourceInfo dataSourceInfo)
    {
        return success(dataSourceInfoService.selectDataSourceInfoById(dataSourceInfo.getId()));
    }

    @PreAuthorize("@ss.hasPermi('console:datasourceInfo:query')")
    @PostMapping("/groupData")
    @ResponseBody
    public AjaxResult groupData(@RequestBody DataSourceInfo dataSourceInfo)
    {
        return success(dataSourceInfoService.groupData(dataSourceInfo));
    }

    /**
     * 新增数据源信息
     */
    @PreAuthorize("@ss.hasPermi('console:datasourceInfo:add')")
    @Log(title = "数据源信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody DataSourceInfo dataSourceInfo)
    {
        dataSourceInfo.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dataSourceInfoService.insertDataSourceInfo(dataSourceInfo));
    }

    /**
     * 修改数据源信息
     */
    @PreAuthorize("@ss.hasPermi('console:datasourceInfo:edit')")
    @Log(title = "数据源信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody DataSourceInfo dataSourceInfo)
    {
        dataSourceInfo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dataSourceInfoService.updateDataSourceInfo(dataSourceInfo));
    }

    /**
     * 删除数据源信息
     */
    @PreAuthorize("@ss.hasPermi('console:datasourceInfo:remove')")
    @Log(title = "数据源信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DataSourceInfo dataSourceInfo)
    {
        return toAjax(dataSourceInfoService.deleteDataSourceInfoByIds(dataSourceInfo.getIds()));
    }

    /**
     * 测试节点是否可用
     * @return
     */
    @Log(title = "数据源信息", businessType = BusinessType.TEST)
    @PostMapping("/databaseCheck")
    @ResponseBody
    public AjaxResult databaseCheck(@RequestBody DataSourceInfo dataSourceInfo){
        return dataSourceInfoService.databaseCheck(dataSourceInfo);
    }

    /**
     * 获取数据库列表
     * @return
     */
    @PostMapping("/listDatabase")
    @ResponseBody
    public AjaxResult dataSourceGetDBS(@RequestBody DataSourceInfo dataSourceInfo){
        return dataSourceInfoService.dataSourceGetDBS(dataSourceInfo);
    }

    /**
     * 获取数据库表列表
     * @return
     */

    @PostMapping("/listTables")
    @ResponseBody
    public AjaxResult dataSourceGetTables(@RequestBody DataSourceInfo dataSourceInfo){
        return dataSourceInfoService.dataSourceGetTables(dataSourceInfo);
    }

    /**
     * 获取数据库对象接口
     * @return
     */

    @Log(title = "数据源信息", businessType = BusinessType.TEST)
    @PostMapping("/listObjects")
    @ResponseBody
    public AjaxResult dataSourceGetObjects(@RequestBody DataSourceInfo dataSourceInfo){
        return dataSourceInfoService.dataSourceGetObjects(dataSourceInfo);
    }


    /**
     * 获取数据库对象接口
     * @return
     */
    @Log(title = "数据源信息", businessType = BusinessType.TEST)
    @PostMapping("/listColumns")
    @ResponseBody
    public AjaxResult dataSourceGetColumns(@RequestBody DataSourceInfo dataSourceInfo){
        return dataSourceInfoService.dataSourceGetColumns(dataSourceInfo);
    }

    /**
     * 获取数据库对象接口
     * @return
     */
    @Log(title = "数据源信息", businessType = BusinessType.TEST)
    @PostMapping("/listDbColumns")
    @ResponseBody
    public AjaxResult dataSourceGetDbColumns(@RequestBody DataSourceInfo dataSourceInfo){
        return dataSourceInfoService.dataSourceGetDbColumns(dataSourceInfo);
    }


    @Log(title = "数据库&schema", businessType = BusinessType.TEST)
    @PostMapping("/listDatabaseAndSchema")
    @ResponseBody
    public AjaxResult listDatabaseAndSchema(@RequestBody DataSourceInfo dataSourceInfo){
        return dataSourceInfoService.listDatabaseAndSchema(dataSourceInfo);
    }

    @Log(title = "数据库字段类型", businessType = BusinessType.TEST)
    @PostMapping("/listConfigDbTypes")
    @ResponseBody
    public AjaxResult listConfigDbTypes(@RequestBody DataSourceInfo dataSourceInfo){
        return dataSourceInfoService.listConfigDbTypes(dataSourceInfo);
    }
}
